
local rects = {
    ac.rect.j_rect('item1'),
    ac.rect.j_rect('item2'),
    ac.rect.j_rect('item3'),
    ac.rect.j_rect('item4'),
}
local area = 2000

local function get_rect_point(rect1,rect2,name)
    local point1 = rect1:get_point()
    local point2 = rect2:get_point()
    local range = (1 - math.sqrt(math.random(100*100))/100) * area
    local angle = point1 / point2
    local dstance = point1 * point2
    point1 = point1 - {angle,-area/2}
    if name=='Boss' then
        point1 = point1 - {angle+90,area/2}
        point1 = point1 - {angle-90,range}
    elseif name=='精英' then
        point1 = point1 - {angle+90,area/2}
        point1 = point1 - {angle-90,math.random(area)}
    elseif name=='普通' then
        point1 = point1 - {angle-90,area/2}
        point1 = point1 - {angle+90,range}
    end
    point1 = point1 - {angle,math.random(dstance)}
    return point1
end

local function move_loop(unit,rect,last)
    local point1 = unit:get_point()
    local point2 = get_rect_point(rect,last,unit.yg_type)
    local time = (point1 * point2)/unit:get('移动速度')
    unit.last_move_point = point2
    if not unit:has_restriction '硬直' and unit:find_buff '中立怪进入战斗'==nil then
        unit:issue_order('smart',unit.last_move_point)
    end
    ac.wait(time*1000,function()
        if unit:is_alive() then
            unit:issue_order('stop')
            ac.wait(math.random(2,6)*1000,function()
                move_loop(unit,rect,last)
            end)
        end
    end)
end

--箱子几率
local unit_type = {
    ['普通'] = {'龙虾怪','海龟'},
    ['精英'] = {'精英大龙虾','深海大王八','深海巨人','深海元素'},
    ['Boss'] = {'巨型九头蛇','超时空大螃蟹'},
}

local function add_yg_unit(key,id)
    local id = id or math.random(#rects)
    local rect = rects[id]
    local last = rects[id%4+1]
    local name = table.random(unit_type[key])
    local point = get_rect_point(rect,last,key)
    local unit = ac.player[16]:create_unit(name,point,math.random(360))
    if unit:is_type('Boss')==false and unit:is_type('精英')==false then
        unit_add_life2(unit)
    end
    jass.RemoveGuardPosition(unit.handle)
    unit.yg_type = key
    unit:add_skill('野怪自适应属性','隐藏')
    --unit:add_restriction '无敌'
    unit:add_restriction '幽灵'
    move_loop(unit,rect,last)
    unit:event '单位-死亡'(function(_,_,killer)
        local player = unit.player or killer.owner
        local hero = player.hero
        add_yg_unit(key)
        if player then
            local ex = unit['随机金币']
            if ex then
                local min,max = ex:match('(%d*)-(%d*)')
                local jb = math.random(min,max)
                unit:add_effect([[Abilities\Spells\Items\ResourceItems\ResourceEffectTarget.mdl]]):remove()
                player:add('金币',jb)
                player:send_msg(('获得|cff00ffff%0.f|r金币'):format(jb))
            end
            --获得道具
            local item_str = unit['奖励表']
            if item_str then
                local items = split_string(item_str,',')
                if unit:is_type('Boss') then
                    ac.each_player(function(player)
                        local hero = player.hero
                        if hero then
                            local name = table.random(items)
                            local new = hero:add_item(name)
                            local text = ('获得|cff00ff00%s|r'):format(new:get_title())
                            player:send_msg(text)
                            barrage(player,text)
                        end
                    end)
                else
                    local name = table.random(items)
                    local new = hero:add_item(name)
                    local text = ('获得|cff00ff00%s|r'):format(new:get_title())
                    player:send_msg(text)
                end
            end
        end
    end)
end

ac.game:event '游戏-设置难度'(function(_,hard)
    for a=1,25 do
        add_yg_unit('普通',a%4+1)
    end

    for a=1,10 do
        add_yg_unit('精英',a%4+1)
    end
    for a=1,4 do
        add_yg_unit('Boss',a)
    end
end)

